package metalink;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
import java.sql.*;

import org.apache.struts2.ServletActionContext;

import vo.*;
import vo.mixed.*;
import model.admin.*;
import myutil.*;
import java.io.*;
import javax.servlet.*;
public class DeleteMessage extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
ArrayList<Message_Receiver_MixedVo> unread=new ArrayList<Message_Receiver_MixedVo>();
ArrayList<Message_Receiver_MixedVo> read=new ArrayList<Message_Receiver_MixedVo>();
ArrayList<Message_Receiver_MixedVo> inbox=new ArrayList<Message_Receiver_MixedVo>();
ArrayList<Message_Receiver_MixedVo> sent=new ArrayList<Message_Receiver_MixedVo>();
ArrayList<Message_Receiver_MixedVo> deleted=new ArrayList<Message_Receiver_MixedVo>();
ArrayList<Message_Receiver_MixedVo> starred=new ArrayList<Message_Receiver_MixedVo>();
private int message_id;
private int inbox_count = 0;
private int read_count = 0;
private int unread_count = 0;
private int starred_count = 0;
private int deleted_count = 0;
private int sent_count = 0;
public ArrayList<Message_Receiver_MixedVo> getUnread() {
	return unread;
}
public void setUnread(ArrayList<Message_Receiver_MixedVo> unread) {
	this.unread = unread;
}
public ArrayList<Message_Receiver_MixedVo> getRead() {
	return read;
}
public void setRead(ArrayList<Message_Receiver_MixedVo> read) {
	this.read = read;
}
public String execute(){
	try {
		String username=((ServletRequest) ServletActionContext.getRequest().getSession()).getAttribute("username").toString();
		Connect c=new Connect();
		c.dml("update message_receiver set deleted_flag=1 where message_id = '"+message_id+"' and receiver_id = '"+username+"'");
		ResultSet rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.read_flag=0 and mr.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			unread.add(single_object);
			unread_count = unread.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.read_flag=1 and mr.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			read.add(single_object);
			read_count = read.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			inbox.add(single_object);
			inbox_count = inbox.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.deleted_flag=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setReceiver_id(rs.getString("receiver_id"));
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			deleted.add(single_object);
			deleted_count = deleted.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.deleted_flag!=1 and mr.starred_flag=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setReceiver_id(rs.getString("receiver_id"));
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			starred.add(single_object);
			starred_count = starred.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and m.sender_id='"+username+"' and m.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setReceiver_id(rs.getString("receiver_id"));
			single_object.setDeleted_flag(rs.getInt("m.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("m.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			sent.add(single_object);
			sent_count = sent.size();
		}
		return SUCCESS;
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return 	ERROR;
	}
}

public String delete_message_sent(){
	try {
		String username=ServletActionContext.getRequest().getSession().getAttribute("username").toString();
		Connect c=new Connect();
		c.dml("update message set deleted_flag=1 where message_id = '"+message_id+"' and sender_id = '"+username+"'");
		ResultSet rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.read_flag=0 and mr.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			unread.add(single_object);
			unread_count = unread.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.read_flag=1 and mr.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			read.add(single_object);
			read_count = read.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			inbox.add(single_object);
			inbox_count = inbox.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.deleted_flag=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setReceiver_id(rs.getString("receiver_id"));
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			deleted.add(single_object);
			deleted_count = deleted.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and mr.receiver_id='"+username+"' and mr.deleted_flag!=1 and mr.starred_flag=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setReceiver_id(rs.getString("receiver_id"));
			single_object.setDeleted_flag(rs.getInt("mr.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("mr.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			starred.add(single_object);
			starred_count = starred.size();
		}
		rs=c.getResult("select * From message m, message_receiver mr where mr.message_id=m.message_id and m.sender_id='"+username+"' and m.deleted_flag!=1 order by sent_at desc");
		while(rs.next()){
			int attachment_id,attachment_count=0;
			Message_Receiver_MixedVo single_object=new Message_Receiver_MixedVo();
			single_object.setMessage_id(rs.getInt("message_id"));
			single_object.setSubject(rs.getString("subject"));
			single_object.setBody(rs.getString("body"));
			single_object.setSender_id(rs.getString("sender_id"));
			single_object.setSent_at(rs.getTimestamp("sent_at").toString());
			single_object.setReceiver_id(rs.getString("receiver_id"));
			single_object.setDeleted_flag(rs.getInt("m.deleted_flag"));
			single_object.setRead_flag(rs.getInt("read_flag"));
			single_object.setStarred_flag(rs.getInt("m.starred_flag"));
			attachment_id=rs.getInt("attachment_id");
			File f=new File("F:/Metalink/attachments/"+attachment_id+"/");
			if(attachment_id!=0)
				attachment_count=f.list().length;
			single_object.setAttachment_count(attachment_count);
			sent.add(single_object);
			sent_count = sent.size();
		}
		return SUCCESS;
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return 	ERROR;
	}

}

public ArrayList<Message_Receiver_MixedVo> getInbox() {
	return inbox;
}
public void setInbox(ArrayList<Message_Receiver_MixedVo> inbox) {
	this.inbox = inbox;
}
public ArrayList<Message_Receiver_MixedVo> getSent() {
	return sent;
}
public void setSent(ArrayList<Message_Receiver_MixedVo> sent) {
	this.sent = sent;
}
public int getInbox_count() {
	return inbox_count;
}
public void setInbox_count(int inbox_count) {
	this.inbox_count = inbox_count;
}
public int getRead_count() {
	return read_count;
}
public void setRead_count(int read_count) {
	this.read_count = read_count;
}
public int getUnread_count() {
	return unread_count;
}
public void setUnread_count(int unread_count) {
	this.unread_count = unread_count;
}
public int getStarred_count() {
	return starred_count;
}
public void setStarred_count(int starred_count) {
	this.starred_count = starred_count;
}
public int getDeleted_count() {
	return deleted_count;
}
public void setDeleted_count(int deleted_count) {
	this.deleted_count = deleted_count;
}
public int getSent_count() {
	return sent_count;
}
public void setSent_count(int sent_count) {
	this.sent_count = sent_count;
}
public ArrayList<Message_Receiver_MixedVo> getDeleted() {
	return deleted;
}
public void setDeleted(ArrayList<Message_Receiver_MixedVo> deleted) {
	this.deleted = deleted;
}
public ArrayList<Message_Receiver_MixedVo> getStarred() {
	return starred;
}
public void setStarred(ArrayList<Message_Receiver_MixedVo> starred) {
	this.starred = starred;
}
public int getMessage_id() {
	return message_id;
}
public void setMessage_id(int message_id) {
	this.message_id = message_id;
}
}
